{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fe12c203-e6a6-452c-a655-afb8a03a4ff5",
   "metadata": {},
   "source": [
    "# End of week 1 exercise\n",
    "\n",
    "To demonstrate your familiarity with OpenAI API, and also Ollama, build a tool that takes a technical question,  \n",
    "and responds with an explanation. This is a tool that you will be able to use yourself during the course!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e291b9d",
   "metadata": {},
   "source": [
    "-> I interpreted \"technical\" as \"related to engineering\" and build a tool to answer questions in the context of choosing the best power plants in energy systems. (In a very simplified way.) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c1070317-3ed9-4659-abe3-828943230e03",
   "metadata": {},
   "outputs": [],
   "source": [
    "# imports\n",
    "import os\n",
    "from dotenv import load_dotenv\n",
    "from IPython.display import Markdown, display, update_display\n",
    "from openai import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4a456906-915a-4bfd-bb9d-57e505c5093f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# constants\n",
    "MODEL_GPT = 'gpt-4o-mini'\n",
    "MODEL_LLAMA = 'llama3.2:1b'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "a8d7923c-5f28-4c30-8556-342d7c8497c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# set up environment\n",
    "week1_ex_system_prompt = \"\"\"\n",
    "You are a Energy Systems and Engineering expert that can recommend the construction \n",
    "of new power plants in Central Europe based on their CAPEX and OPEX and only based on this.\n",
    "You show in clear, consise and structured ways, why and when you prefer one type of power plant over another \n",
    "sticking to the cost argument as a main factor. \n",
    "You are undogmatic and science-based. \n",
    "\"\"\"\n",
    "\n",
    "def answer_question(sel_api_key, selected_model, question, sel_base_url=None):\n",
    "    llm = OpenAI(base_url=sel_base_url, api_key=sel_api_key)\n",
    "    stream = llm.chat.completions.create(\n",
    "        model=selected_model,\n",
    "        messages=[\n",
    "            {'role': 'user', 'content':question},\n",
    "            {'role':'system', 'content':week1_ex_system_prompt}\n",
    "        ], \n",
    "        stream=True\n",
    "    )\n",
    "    response = \"\"\n",
    "    display_handle = display(Markdown(\"\"), display_id=True)\n",
    "    for chunk in stream:\n",
    "        response += chunk.choices[0].delta.content or ''\n",
    "        update_display(Markdown(response), display_id=display_handle.display_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "b41dfb23",
   "metadata": {},
   "outputs": [],
   "source": [
    "final_question = \"\"\"\n",
    "Does it make more sense to build solar and wind power plants \n",
    "or nuclear power plants in France? Be specific about the conditions in the country and answer in 10 sentences.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60ce7000-a4a5-4cce-a261-e75ef45063b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get gpt-4o-mini to answer, with streaming\n",
    "load_dotenv(override=True)\n",
    "answer_question(sel_api_key=os.getenv('OPENAI_API_KEY'),selected_model=MODEL_GPT, question=final_question)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7f12f622",
   "metadata": {},
   "outputs": [],
   "source": [
    "!ollama pull llama3.2:1b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f7c8ea8-4082-4ad0-8751-3301adcf6538",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get Llama 3.2 to answer\n",
    "answer_question(sel_base_url=\"http://localhost:11434/v1\", sel_api_key='ollama',selected_model=MODEL_LLAMA, question=final_question)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
